package com.example.Lezedata.ui.activity.setting;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.activity.common.TransitionPage;
import com.example.Lezedata.ui.activity.login.SetPsdVc;
import com.example.Lezedata.ui.fragment.commonfragment.inputTextField;
import com.example.Lezedata.util.GBToast;
import com.example.Lezedata.util.GBUtil;

import bid.Bid;

public class ForgetPINVc extends GBBaseVc {
    private TextView rightView;
    private inputTextField textField1;
    private inputTextField textField2;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_p_i_n_vc;
    }

    @Override
    public CharSequence title() {
        return "Forgotten PIN";
    }

    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        findViewById(R.id.commit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
        initView();
    }

    void initView (){
        textField1 = (inputTextField)getSupportFragmentManager().findFragmentById(R.id.mobile);
        textField1.mViewModel.code.setValue("+234");
        textField1.mViewModel.hint.setValue(getString(R.string.EnterMobile));

        textField2 = (inputTextField)getSupportFragmentManager().findFragmentById(R.id.password);
        textField2.mViewModel.code.setValue(getString(R.string.Code));
        textField2.mViewModel.hint.setValue(getString(R.string.EnterCode));
        textField2.customViewCreateListener = new inputTextField.RightCustomViewCreateListener() {
            @Override
            public void createView(TextView textView) {
                rightView = textView;
                refreshRightView();
                FrameLayout.LayoutParams viewParams = (FrameLayout.LayoutParams) rightView.getLayoutParams();
                viewParams.rightMargin = GBUtil.dpTo2px(mContext,20);
                viewParams.width = GBUtil.dpTo2px(mContext,77);
                viewParams.height = GBUtil.dpTo2px(mContext,38);

                rightView.setLayoutParams(viewParams);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        sendCode(v);
                    }
                });
            }
        };
    }

    void refreshRightView(){
        rightView.setGravity(Gravity.CENTER);
        rightView.setText("GET");
        rightView.setTextColor(getResources().getColor(R.color.app_color_main));
        rightView.setTextSize(16);

        Drawable drawable = getResources().getDrawable(R.drawable.corner_store_miancolor_19dp);
        rightView.setBackground(drawable);

    }

    void sendCode(View view){
        String phone = textField1.inputText().toString();
        String phoneCode = textField1.code().toString();
        if (phone.isEmpty()){
            GBToast.shortToast("phone not null");
            return;
        }
        Bid.ForgetPwRequest request = Bid.ForgetPwRequest.newBuilder()
                .setPhone(phone)
                .setPhoneCode(phoneCode)
                .build();
        GBNetRequest.stub().forgetTransPw(request, ServerResponseObserver.observer(new ServerResponseObserver.requestListener() {
            @Override
            public void complete(Object value, GBError error) {
                if (error == null){
                    GBToast.shortToast("SMS sent successfully");
                }
            }
        }));
    }

    void submit() {
        String phone = textField1.inputText().toString();
//        String phoneCode = textField1.code().toString();
        String code = textField2.inputText().toString();
        if (phone.isEmpty()) {
            GBToast.shortToast("phone not null");
            return;
        }
        if (code.isEmpty()) {
            GBToast.shortToast("code not null");
            return;
        }

        Intent intent = new Intent(mContext, PinSettingVc.class);
        intent.putExtra("code", code);
        jumpActivityIntent(intent);

    }
}